home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 1
/
Nebula One.iso
/
Mail
/
pine3.92
/
pine
/
osdep
/
pipe.os2
< prev
next >
Wrap
Text File
|
1996-03-14
|
6KB
|
166 lines
#line 2 "osdep/pipe.os2"
/*======================================================================
pipe
Initiate I/O to and from a process. These functions are similar to
popen and pclose, but both an incoming stream and an output file are
provided.
====*/
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
short child_signalled;
short child_jump = 0;
jmp_buf child_state;
/*----------------------------------------------------------------------
Spawn a child process and optionally connect read/write pipes to it
Args: command -- string to hand the shell
outfile -- address of pointer containing file to receive output
errfile -- address of pointer containing file to receive error output
mode -- mode for type of shell, signal protection etc...
Returns: pointer to alloc'd PIPE_S on success, NULL otherwise
This is a much simpler version than the UNIX pipe since it utilises
the EMX popen()/pclose() functions for OS/2 (and thereby avoides
fork() and its restrictions under OS/2). It is therefore not as
functional as the UNIX equivalents, and only understands a subset
of modes. All commands also invoke the system shell, but this is
of little consequence as there are no security problems and so forth
in the OS/2 environment.
PIPE_WRITE tells us we need to open a pipe to write the child's
stdin.
PIPE_READ tells us we need to open a pipe to read from the child's
stdout/stderr. *NOTE* Having neither of the above set means
we're not setting up any pipes, just forking the child and exec'ing
the command. Also, this takes precedence over any named outfile.
PIPE_STDERR means we're to tie the childs stderr to the same place
stdout is going. *NOTE* This only makes sense then if PIPE_READ
or an outfile is provided. Also, this takes precedence over any
named errfile.
PIPE_PROT means to protect the child from the usual nasty signals
that might cause premature death. Otherwise, the default signals are
set so the child can deal with the nasty signals in its own way.
PIPE_RESET means we reset the terminal mode to what it was before
we started pine and then exec the command.
----*/
PIPE_S *
open_system_pipe(command, outfile, errfile, mode)
char *command;
char **outfile, **errfile;
int mode;
{
PIPE_S *syspipe = NULL;
FILE *pipefp = NULL;
dprint(5, (debugfile, "Opening pipe: \"%s\" (%s%s%s%s%s%s)\n",command,
(mode & PIPE_WRITE) ? "W":"", (mode & PIPE_READ) ? "R":"",
(mode & PIPE_NOSHELL) ? "N":"", (mode & PIPE_PROT) ? "P":"",
(mode & PIPE_USER) ? "U":"", (mode & PIPE_RESET) ? "T":""));
if(outfile && !*outfile)
*outfile = temp_nam(NULL, "pp"); /* asked for, but not named? */
if(errfile && !*errfile)
*errfile = temp_nam(NULL, "pp"); /* ditto */
syspipe = (PIPE_S *)fs_get(sizeof(PIPE_S));
memset(syspipe, 0, sizeof(PIPE_S));
if(mode & PIPE_READ){
/* save std handles */
int wasstdin = dup(STDIN_FILENO);
int wasstderr = dup(STDERR_FILENO);
int input = open("NUL", S_IREAD); /* safety */
int outerr = creat(errfile ? *errfile : "NUL", S_IREAD|S_IWRITE);
/* dup the handles */
dup2(outerr, STDERR_FILENO);
dup2(input, STDIN_FILENO);
/* now, start the pipe */
pipefp = syspipe->ifilep = popen(command, "rb");
/* revert back to the previous handles
dup2(STDIN_FILENO, wasstdin);
dup2(STDERR_FILENO, wasstderr);
/* and clean up the no-longer needed handles */
close(input);
close(outerr);
}
else {
/* save std handles */
int wasstdout = dup(STDOUT_FILENO);
int wasstderr = dup(STDERR_FILENO);
/* dup to force child to inheret them */
int output = creat(outfile ? *outfile : "NUL", S_IREAD|S_IWRITE);
int outerr = (mode & PIPE_STDERR) ? output : creat(errfile ? *errfile : "NUL", S_IREAD|S_IWRITE);
dup2(output, STDOUT_FILENO);
dup2(outerr, STDERR_FILENO);
/* Now, open the pipe */
pipefp = syspipe->ofilep = popen(command, "wb");
/* Restore parent's std filehandles and clean up */
dup2(STDOUT_FILENO, wasstdout);
dup2(STDERR_FILENO, wasstderr);
close(wasstdout);
close(wasstderr);
close(output);
if (outerr != output)
close(outerr);
}
if (pipefp == NULL) {
q_status_message1(SM_ORDER,3,3, "Error executing external command: %s",
error_description(errno));
fs_give((void **)&syspipe);
if(outfile)
fs_give((void **)outfile);
dprint(1, (debugfile, "CAN'T OPEN PIPE TO COMMAND: %s\n", command));
}
if (!(mode & (PIPE_WRITE|PIPE_READ))) {
flush_status_messages(0); /* just clean up display */
ClearScreen();
fflush(stdout);
return syspipe;
}
return(syspipe);
}
/*----------------------------------------------------------------------
Close pipe previously allocated and wait for child's death
Args: syspipe -- address of pointer to struct returned by open_system_pipe
Returns: returns exit status of child or -1 if invalid syspipe
----*/
int
close_system_pipe(syspipe)
PIPE_S **syspipe;
{
int status = 0;
if(!syspipe || !*syspipe)
return(-1);
if((*syspipe)->ofilep)
status = pclose((*syspipe)->ofilep);
if((*syspipe)->ifilep)
status = pclose((*syspipe)->ifilep);
ClearScreen(); /* No I/O to forked child */
ps_global->mangled_screen = 1;
fs_give((void **)syspipe);
return(status);
}